﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcVehicleInsurance.Models;

namespace MvcVehicleInsurance.Areas.Admin.Controllers
{
    public class CompanyController : Controller
    {
        private DataClassesVehicleDataContext data = new DataClassesVehicleDataContext();
        //
        // GET: /Admin/Company/

        public ActionResult Index()
        {
            List<MvcVehicleInsurance.Models.CompanyExpense> listcpe = (from cpe in data.CompanyExpenses select cpe).ToList();
            if (listcpe.Count > 0)
            {
                return View(listcpe);
            }

            return View();
        }

        //
        // GET: /Admin/Company/Details/5

        public ActionResult Details(int id)
        {
            CompanyExpense details_cpe = data.CompanyExpenses.Where(get => get.CompanyID == id).First();

            if (ModelState.IsValid)
            {
                return View(details_cpe);
            }
            return View();
        }

        //
        // GET: /Admin/Company/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Admin/Company/Create

        [HttpPost]
        public ActionResult Create(CompanyExpense collection, [Bind(Exclude = "CompanyID")] CompanyExpense cpe)
        {
            if (ModelState.IsValid)
            {
                data.CompanyExpenses.InsertOnSubmit(collection);
                data.SubmitChanges();
            }
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/Company/Edit/5

        public ActionResult Edit(int id)
        {
            CompanyExpense edit_cpe = data.CompanyExpenses.First(lx => lx.CompanyID == id);
            return View(edit_cpe);
        }

        //
        // POST: /Admin/Company/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, CompanyExpense collection)
        {
            CompanyExpense edit_cpe = data.CompanyExpenses.First(s => s.CompanyID == id);
            if (ModelState.IsValid)
            {
                edit_cpe.CompanyID = collection.CompanyID;
                edit_cpe.dateOfExpense = collection.dateOfExpense;
                edit_cpe.typeOfExpense = collection.typeOfExpense;
                edit_cpe.amountOfExpense = collection.amountOfExpense;
                data.SubmitChanges();
            }

            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        public ActionResult Delete(int id)
        {
            CompanyExpense edit_cpe = data.CompanyExpenses.First(ds => ds.CompanyID == id);
            if (ModelState.IsValid)
            {
                data.CompanyExpenses.DeleteOnSubmit(edit_cpe);
                data.SubmitChanges();
            }

            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
